package com.nageoffer.shortlink.project.config;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 布隆过滤器配置
 */
@Configuration
public class RBloomFilterConfiguration {

    /**
     * 防止短链接创建 查询数据库的布隆过滤器
     */
    @Bean
    public RBloomFilter<String> shortUriCreateCachePenetrationBloomFilter(RedissonClient redissonClient) {
        RBloomFilter<String> cachePenetrationBloomFilter = redissonClient.getBloomFilter("RBloomFilterConfiguration");
        // expectedInsertions：预估布隆过滤器存储的元素长度，这里为 1亿元素；
        // falseProbability：运行的误判率，这里为千分之一
        cachePenetrationBloomFilter.tryInit(100000000L, 0.001);      // 用布隆过滤器进行初始化，这个参数大概占用171.394MB的Redis内存

        return cachePenetrationBloomFilter;
    }
}
